package gui;

import dados.BancoQuestoes;
import dados.Questao;
import gui.modelos.KeyListenerJanela;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import logica.GerenciaBanco;

/**
 *
 * @author  UltraXP
 */
public class NotasProporcao extends javax.swing.JDialog {
	double[] notasQuestoes = null;
    double[][] notasTestes = null;
    
    /** Creates new form NotasProporcao */
    public NotasProporcao(java.awt.Frame parent) {
        super(parent, true);
        setTitle("Proporção das Notas");
        initComponents();
        Janelas.alinharContainer(this);
        iniciarCampos();
        this.addKeyListener(new KeyListenerJanela());
    }
    
    private void iniciarCampos() {
        double somaQuestoes = 0, somaTestes = 0;
        int nroQuestoes = BancoQuestoes.getInstancia().getTamanhoBanco();
        String[] vetorQuestao = new String[nroQuestoes];
        String[] vetorTeste = null;
        int maxTestes = 0;
        
        notasQuestoes = new double[nroQuestoes];
        
        for (int i = 0; i <= nroQuestoes - 1; i++) {
            int tam = BancoQuestoes.getInstancia()
                    .getArrayListQuestaoProva()
                    .get(i)
                    .getQuestao(0)
                    .getTestes()
                    .size();
            if (tam > maxTestes) {
                maxTestes = tam;
            }
        }
        
        notasTestes = new double[nroQuestoes][maxTestes];
        
        for (int i = 0; i <= nroQuestoes - 1; i++) {
            vetorQuestao[i] = "Questão " + (i + 1);
            notasQuestoes[i] = BancoQuestoes.getInstancia()
                    .getArrayListQuestaoProva()
                    .get(i)
                    .getQuestao(0)
                    .getNotaMax();
            int nroTestes = BancoQuestoes.getInstancia()
                    .getArrayListQuestaoProva()
                    .get(i)
                    .getQuestao(0)
                    .getTestes()
                    .size();
            vetorTeste = new String[nroTestes];
            for (int j = 0; j <= nroTestes - 1; j++) {
                vetorTeste[j] = "Teste " + (j + 1);
                notasTestes[i][j] = BancoQuestoes.getInstancia()
                    .getArrayListQuestaoProva()
                    .get(i)
                    .getQuestao(0)
                    .getTeste(j)
                    .getPorcentagemNotaMax();
            }
        }
        DefaultComboBoxModel modelQuestao = new DefaultComboBoxModel(vetorQuestao);        
        cmbQuestao.setModel(modelQuestao);
        cmbQuestao.setSelectedIndex(0);
        txtMaxQuestao.setText("" + ( (int)BancoQuestoes.getInstancia()
                                            .getArrayListQuestaoProva()
                                            .get(0)
                                            .getQuestao(0)
                                            .getNotaMax()));
        for (int i = 0; i <= notasQuestoes.length - 1; i++) {
            somaQuestoes += notasQuestoes[i];
        }        
        txtSomaQuestoes.setText("" + ((int)somaQuestoes));
        
        GerenciaBanco ger = new GerenciaBanco(BancoQuestoes.getInstancia()
                                            .getArrayListQuestaoProva());
        DefaultComboBoxModel modelTeste = new DefaultComboBoxModel(ger.getVetorTestes(0));
        cmbTeste.setModel(modelTeste);
        cmbTeste.setSelectedIndex(0);
        txtMaxTeste.setText("" + ((int)BancoQuestoes.getInstancia()
                                        .getArrayListQuestaoProva()
                                        .get(0)
                                        .getQuestao(0)
                                        .getTeste(0)
                                        .getPorcentagemNotaMax()));
        for (int i = 0; i <= notasTestes[0].length - 1; i++) {
            somaTestes += notasTestes[0][i];
        }
        txtSomaTestes.setText("" + ((int)somaTestes));
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnConfirmar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cmbQuestao = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        txtSomaQuestoes = new javax.swing.JTextField();
        txtMaxQuestao = new gui.modelos.TextoNumeros();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        cmbTeste = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtSomaTestes = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtMaxTeste = new gui.modelos.TextoNumeros();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnConfirmar.setText("Confirmar");
        btnConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Questões"));

        jLabel2.setText("Nota Máxima da Questão");

        jLabel1.setText("Número da Questão");

        cmbQuestao.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbQuestaoItemStateChanged(evt);
            }
        });

        jLabel3.setText("Soma das Questões");

        txtSomaQuestoes.setEditable(false);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel2)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(txtMaxQuestao, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(cmbQuestao, 0, 93, Short.MAX_VALUE))
                        .add(5, 5, 5))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .add(36, 36, 36)
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(txtSomaQuestoes, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(cmbQuestao, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(txtMaxQuestao, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(53, 53, 53)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(txtSomaQuestoes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(41, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Testes da Quest�o Selecionada"));

        jLabel4.setText("Número do Teste");

        cmbTeste.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbTesteItemStateChanged(evt);
            }
        });

        jLabel5.setText("Porcentagem do Teste");

        jLabel6.setText("Soma dos Testes");

        txtSomaTestes.setEditable(false);

        jLabel7.setText("%");

        jLabel8.setText("%");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(19, 19, 19)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                        .add(25, 25, 25)
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cmbTeste, 0, 94, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2Layout.createSequentialGroup()
                                .add(26, 26, 26)
                                .add(jLabel6)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(txtSomaTestes, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE))
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2Layout.createSequentialGroup()
                                .add(jLabel5)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(txtMaxTeste, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel7)
                            .add(jLabel8))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cmbTeste, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jLabel7)
                    .add(txtMaxTeste, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(52, 52, 52)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(jLabel8)
                    .add(txtSomaTestes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(42, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(btnCancelar)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 353, Short.MAX_VALUE)
                        .add(btnConfirmar))
                    .add(layout.createSequentialGroup()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnCancelar)
                    .add(btnConfirmar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtMaxTesteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMaxTesteKeyReleased
        int soma = 0;
        int questaoSelecionada = cmbQuestao.getSelectedIndex();
        int testeSelecionado = cmbTeste.getSelectedIndex();
        int nroTestes = BancoQuestoes.getInstancia()
                .get(questaoSelecionada)
                .getQuestao(0)
                .getTestes()
                .size();
        try {
            notasTestes[questaoSelecionada][testeSelecionado] = Integer.parseInt(txtMaxTeste.getText());
            for (int i = 0; i <= nroTestes - 1; i++) {
                soma += notasTestes[questaoSelecionada][i];
            }
            txtSomaTestes.setText("" + (int)soma);
        } catch (NumberFormatException ex) {
            // ignorando exce��o
        }
    }//GEN-LAST:event_txtMaxTesteKeyReleased

    private void cmbTesteItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbTesteItemStateChanged
        int questaoSelecionada = cmbQuestao.getSelectedIndex();
        int testeSelecionado = cmbTeste.getSelectedIndex();
        txtMaxTeste.setText("" + (int)notasTestes[questaoSelecionada][testeSelecionado]);
    }//GEN-LAST:event_cmbTesteItemStateChanged

    private void txtMaxQuestaoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMaxQuestaoKeyReleased
        int soma = 0;
        int selecionado = cmbQuestao.getSelectedIndex();
        try {
            notasQuestoes[selecionado] = Integer.parseInt(txtMaxQuestao.getText());
            for (int i = 0; i <= notasQuestoes.length - 1; i++) {
                soma += notasQuestoes[i];
            }
            txtSomaQuestoes.setText("" + (int)soma);
        } catch (NumberFormatException ex) {
            // ignorando exce��o
        }
    }//GEN-LAST:event_txtMaxQuestaoKeyReleased

    private void cmbQuestaoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbQuestaoItemStateChanged
        int selecionado = cmbQuestao.getSelectedIndex();
        int somaTestes = 0;
        txtMaxQuestao.setText("" + (int)notasQuestoes[selecionado]);
        GerenciaBanco ger = new GerenciaBanco(BancoQuestoes.getInstancia().getArrayListQuestaoProva());
        DefaultComboBoxModel modelTeste = new DefaultComboBoxModel(ger.getVetorTestes(selecionado));
        cmbTeste.setModel(modelTeste);
        cmbTeste.setSelectedIndex(0);
        txtMaxTeste.setText("" + (int)notasTestes[selecionado][0]);
        for (int i = 0; i <= notasTestes[selecionado].length - 1; i++) {
            somaTestes += notasTestes[selecionado][i];
        }
        txtSomaTestes.setText("" + (int)somaTestes);
    }//GEN-LAST:event_cmbQuestaoItemStateChanged

    private void btnConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmarActionPerformed
        int somaQuestoes = 0;
        int opcao = 0;
        for (int i = 0; i <= notasQuestoes.length - 1; i++) {
            somaQuestoes += notasQuestoes[i];
        }
        
        if (somaQuestoes != 100) {
            opcao = JOptionPane.showConfirmDialog(this, "A soma das questões não totaliza " +
                "100.\n Você quer continuar mesmo assim?", "Confirmação!", JOptionPane.YES_NO_OPTION);
        }
        if (opcao == 0) {
            
            for (int i = 0; i <= notasQuestoes.length - 1; i++) {
                int nroTestes = BancoQuestoes.getInstancia()
                        .getArrayListQuestaoProva()
                        .get(i)
                        .getQuestao(0)
                        .getTestes()
                        .size();
                int soma = 0;
                for (int j = 0; j <= nroTestes - 1; j++) {
                    soma += notasTestes[i][j];
                }
                if (soma != 100) {
                    JOptionPane.showMessageDialog(this, "A soma dos testes da questão " + (i + 1) + " não totaliza " +
                "100.\n é necessário alterar.", "Erro!", JOptionPane.YES_NO_OPTION);
                    return;
                }
            }
            
            for (int i = 0; i <= notasQuestoes.length - 1; i++) {
                for(Questao questao: BancoQuestoes.getInstancia()
                                        .getArrayListQuestaoProva()
                                        .get(i)
                                        .getArrayListQuestao()) {
                    questao.setNotaMax(notasQuestoes[i]);
                }
                int nroTestes = BancoQuestoes.getInstancia()
                                    .get(i)
                                    .getQuestao(0)
                                    .getTestes()
                                    .size();
                for(Questao questao: BancoQuestoes.getInstancia()
                                        .getArrayListQuestaoProva()
                                        .get(i)
                                        .getArrayListQuestao()) {
                    for (int j = 0; j <= nroTestes - 1; j++) {
                        questao.getTeste(j).setPorcentagemNotaMax(notasTestes[i][j]);
                    }
                }
            }
            JOptionPane.showMessageDialog(null, "Notas salvas com sucesso!", 
                    "Notas Salvas!", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        }
    }//GEN-LAST:event_btnConfirmarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed
       
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConfirmar;
    private javax.swing.JComboBox cmbQuestao;
    private javax.swing.JComboBox cmbTeste;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private gui.modelos.TextoNumeros txtMaxQuestao;
    private gui.modelos.TextoNumeros txtMaxTeste;
    private javax.swing.JTextField txtSomaQuestoes;
    private javax.swing.JTextField txtSomaTestes;
    // End of variables declaration//GEN-END:variables
    
}
